//
//  Judge.m
//  YouthHostel
//
//  Created by administrator on 16/10/12.
//  Copyright © 2016年 Lorain mac pro. All rights reserved.
//

#import "Judge.h"
#import "Define.h"
#import "TggStarEvaluationView.h"
@interface Judge ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UITextViewDelegate>
//导航栏
@property (strong,nonatomic)UIView *nav;
//标题
@property (strong,nonatomic)UILabel *titleLab;
//返回
@property (strong,nonatomic)UIButton *backBtn;

@property (strong,nonatomic)UITextView *judgeText;

@property (strong,nonatomic)UILabel *lab;

@property (strong,nonatomic)UIButton *photoBtn;

@property (strong,nonatomic)UIButton *setJudge;
@property (strong,nonatomic)UILabel *line1;
@property (strong,nonatomic)UILabel *line2;
@property (strong,nonatomic)UILabel *line3;

@property (strong,nonatomic)UIButton *goodbtn;
@property (strong,nonatomic)UIButton *commonBtn;
@property (strong,nonatomic)UIButton *badBtn;

@property (strong,nonatomic)UILabel *addLab;

@property (assign,nonatomic)int count;
@property (assign,nonatomic)int newcount;

@property (strong,nonatomic)UILabel *num1;
@property (strong,nonatomic)UILabel *num2;
@property (strong,nonatomic)UILabel *num3;

@property (weak ,nonatomic) TggStarEvaluationView *tggStarEvaView1;
@property (weak ,nonatomic) TggStarEvaluationView *tggStarEvaView2;
@property (weak ,nonatomic) TggStarEvaluationView *tggStarEvaView3;

@property (assign,nonatomic)int i ;
@end

@implementation Judge

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.count = 45;
    self.newcount = 0;
    
    self.nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.nav.backgroundColor = [UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1];
    
    self.titleLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, WIDTH-200, 34)];
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.text = @"发表评价";
    self.titleLab.textColor = [UIColor whiteColor];
    self.titleLab.font = [UIFont systemFontOfSize:20];
    
    self.backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 25, 25, 25)];
    [self.backBtn setImage:[UIImage imageNamed:@"后退"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification" object:_judgeText];
    
    self.lab = [[UILabel alloc]initWithFrame:CGRectMake(3, 64, 200, 30)];
    self.lab.text = @"请输入评论";
    self.lab.textColor = [UIColor lightGrayColor];
    self.lab.enabled = NO;
    self.lab.textAlignment = NSTextAlignmentLeft;
    
    self.judgeText = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 150)];
    self.judgeText.font = [UIFont systemFontOfSize:15];
    self.judgeText.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.judgeText.backgroundColor = [UIColor whiteColor];
    self.judgeText.delegate = self;
    
    self.addLab = [[UILabel alloc]initWithFrame:CGRectMake(50, 214, 60, 40)];
    self.addLab.text = @"添加照片";
    self.addLab.textColor = [UIColor lightGrayColor];
    self.addLab.font = [UIFont systemFontOfSize:13];
    
    self.photoBtn = [[UIButton alloc]initWithFrame:CGRectMake(5, 214, 40, 40)];
    [self.photoBtn setTitle:@"+" forState:0];
    [self.photoBtn setTitleColor:[UIColor grayColor] forState:0];
    self.photoBtn.titleLabel.font = [UIFont systemFontOfSize:35];
    self.photoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.photoBtn.layer.borderWidth = 1;
    self.photoBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.photoBtn addTarget:self action:@selector(alterHeadPortrait:) forControlEvents:UIControlEventTouchUpInside];

    
    self.setJudge = [[UIButton alloc]initWithFrame:CGRectMake(0, HEIGHT-49, WIDTH, 49)];
//    self.setJudge.backgroundColor = [UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1];
    [self.setJudge setTitle:@"提交评价" forState:0];
    [self.setJudge setTitleColor:[UIColor whiteColor] forState:0];
    self.setJudge.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.setJudge addTarget:self action:@selector(setjudge:) forControlEvents:UIControlEventTouchUpInside];
    self.setJudge.backgroundColor = [UIColor grayColor];
    self.setJudge.enabled = NO;

    self.line1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 210, WIDTH, 1)];
    self.line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.line2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 260, WIDTH, 1)];
    self.line2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.line3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 310, WIDTH, 1)];
    self.line3.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.goodbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 260, WIDTH/3, 50)];
    [self.goodbtn setTitle:@"好评" forState:0];
    [self.goodbtn setTitleColor:[UIColor blackColor] forState:0];
    [self.goodbtn addTarget:self action:@selector(good:) forControlEvents:UIControlEventTouchUpInside];
    self.goodbtn.tag = 1001;
    
    self.commonBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH/3, 260, WIDTH/3, 50)];
    [self.commonBtn setTitle:@"中评" forState:0];
    [self.commonBtn setTitleColor:[UIColor blackColor] forState:0];
      [self.commonBtn addTarget:self action:@selector(good:) forControlEvents:UIControlEventTouchUpInside];
    self.commonBtn.tag = 1002;
    
    self.badBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH/3*2, 260, WIDTH/3, 50)];
    [self.badBtn setTitle:@"差评" forState:0];
    [self.badBtn setTitleColor:[UIColor blackColor] forState:0];
      [self.badBtn addTarget:self action:@selector(good:) forControlEvents:UIControlEventTouchUpInside];
    self.badBtn.tag = 1003;
    
    self.num1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 330, 100, 30)];
    self.num1.text = @"服务态度";
    self.num1.textColor = [UIColor blackColor];
    self.num1.font = [UIFont systemFontOfSize:18];
    
    self.num2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 380, 100, 30)];
    self.num2.text = @"环境卫生";
    self.num2.textColor = [UIColor blackColor];
    self.num2.font = [UIFont systemFontOfSize:18];
    
    self.num3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 430, 100, 30)];
    self.num3.text = @"设施完善";
    self.num3.textColor = [UIColor blackColor];
    self.num3.font = [UIFont systemFontOfSize:18];
    
    self.i = 0;
    
    [self.view addSubview:_nav];
    [self.nav addSubview:_titleLab];
    [self.nav addSubview:_backBtn];

    
    [self.view addSubview:_judgeText];
    [self.view addSubview:_lab];
    [self.view addSubview:_addLab];
    [self.view addSubview:_photoBtn];
    [self.view addSubview:_setJudge];
    [self.view addSubview:_line1];
    [self.view addSubview:_line2];
    [self.view addSubview:_line3];
    [self.view addSubview:_goodbtn];
    [self.view addSubview:_commonBtn];
    [self.view addSubview:_badBtn];
    
    [self.view addSubview:_num1];
    [self.view addSubview:_num2];
    [self.view addSubview:_num3];
    
    // 注意weakSelf
    __weak __typeof(self)weakSelf = self;
    // 初始化
    self.tggStarEvaView1 = [TggStarEvaluationView evaluationViewWithChooseStarBlock:^(NSUInteger count) {
//        // 做评星后点处理
//        [weakSelf something];
    }];

    self.tggStarEvaView2 = [TggStarEvaluationView evaluationViewWithChooseStarBlock:^(NSUInteger count) {
        //        // 做评星后点处理
        //        [weakSelf something];
    }];
    
    self.tggStarEvaView3 = [TggStarEvaluationView evaluationViewWithChooseStarBlock:^(NSUInteger count) {
        //        // 做评星后点处理
        //        [weakSelf something];
    }];
    
    self.tggStarEvaView1.frame = (CGRect){100,330,23 * 10,30};
    // 设置初始星星数量
     self.tggStarEvaView1.starCount = 0;
    
    // 星星之间的间距，默认0.5
     self.tggStarEvaView1.spacing = 0.3;
    
    
    self.tggStarEvaView2.frame = (CGRect){100,380,23 * 10,30};
    // 设置初始星星数量
    self.tggStarEvaView2.starCount = 0;
    
    // 星星之间的间距，默认0.5
    self.tggStarEvaView2.spacing = 0.3;
    
    self.tggStarEvaView3.frame = (CGRect){100,430,23 * 10,30};
    // 设置初始星星数量
    self.tggStarEvaView3.starCount = 0;
    
    // 星星之间的间距，默认0.5
    self.tggStarEvaView3.spacing = 0.3;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popAndPushPickerView)];
    tapGestureRecognizer.numberOfTapsRequired = 1; // 设置单击几次才触发方法
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    [self.view addSubview:self.tggStarEvaView1];
    [self.view addSubview:self.tggStarEvaView2];
    [self.view addSubview:self.tggStarEvaView3];

    
}
//点击空白收起键盘
- (void)popAndPushPickerView{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        
}
-(void)textViewDidChange:(UITextView *)textView
{
    self.judgeText.text =  textView.text;
    if (textView.text.length == 0) {
        self.lab.text = @"请输入评论";
    }else{
         self.lab.text = @"";
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (_i == 1) {
        self.setJudge.backgroundColor = [UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1];
        self.setJudge.enabled = YES;
    }else{
        self.setJudge.backgroundColor = [UIColor grayColor];
        self.setJudge.enabled = NO;
    }
    
}
- (void)good:(UIButton *)sender
{
    if (sender.tag == 1001) {
        [self.goodbtn setTitleColor:[UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1] forState:0];
        [self.badBtn setTitleColor:[UIColor blackColor] forState:0];
        [self.commonBtn setTitleColor:[UIColor blackColor] forState:0];
    }else if (sender.tag == 1002)
    {
        [self.commonBtn setTitleColor:[UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1] forState:0];
        [self.goodbtn setTitleColor:[UIColor blackColor] forState:0];
        [self.badBtn setTitleColor:[UIColor blackColor] forState:0];
    }else{
        [self.badBtn setTitleColor:[UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1] forState:0];
        [self.goodbtn setTitleColor:[UIColor blackColor] forState:0];
        [self.commonBtn setTitleColor:[UIColor blackColor] forState:0];
    }
    
    self.i = 1;
    if (![_judgeText.text isEqualToString:@""]) {
        self.setJudge.backgroundColor = [UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1];
        self.setJudge.enabled = YES;
    }else{
        self.setJudge.backgroundColor = [UIColor grayColor];
        self.setJudge.enabled = NO;
    }
}

- (void)setjudge:(UIButton *)sender
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"感谢您的评价!" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert addAction:action];
}

- (void)back:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)textFiledEditChanged:(NSNotification *)obj
{
UITextField *textField = (UITextField *)obj.object;
NSString *toBeString = self.judgeText.text;

//获取高亮部分
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
if (!position)
{
    if (toBeString.length > 400)
    {
        NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:400];
        if (rangeIndex.length == 1)
        {
            _judgeText.text = [toBeString substringToIndex:400];
        }
        else
        {
            NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 400)];
            textField.text = [toBeString substringWithRange:rangeRange];
        }
    }
}// 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
else
{
    if (toBeString.length > 400)
    {
        NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:400];
        if (rangeIndex.length == 1)
        {
            _judgeText.text = [toBeString substringToIndex:400];
        }
        else
        {
            NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 400)];
            textField.text = [toBeString substringWithRange:rangeRange];
        }
    }
}
}

-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
    if (self.newcount == 135) {
        UIAlertController *photoAlert = [UIAlertController alertControllerWithTitle:@"最多只能上传三张照片" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [photoAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:photoAlert animated:YES completion:nil];
    }
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //按钮：从相册选择，类型：UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        PickerImage.allowsEditing = YES;
        PickerImage.delegate = self;
        //页面跳转
        PickerImage.videoQuality = UIImagePickerControllerQualityTypeLow;
            [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    
    //按钮：拍照，类型：UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        
        //获取方式:通过相机
        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
        PickerImage.allowsEditing = YES;
        PickerImage.delegate = self;
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放选择的图片。
    
    
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    UIImageView *myImg = [[UIImageView alloc]initWithFrame:CGRectMake(_newcount+5, 214, 40, 40)];
    myImg.image =newPhoto;
    
    UIButton *photo = [[UIButton alloc]initWithFrame:CGRectMake(_count+5, 214, 40, 40)];
    [photo setTitle:@"+" forState:0];
    [photo setTitleColor:[UIColor grayColor] forState:0];
    photo.titleLabel.font = [UIFont systemFontOfSize:35];
    photo.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    photo.layer.borderWidth = 1;
    photo.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [photo addTarget:self action:@selector(alterHeadPortrait:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:photo];
    
    self.addLab.hidden = YES;
    self.photoBtn.hidden = YES;
    self.newcount = _newcount +45;
    self.count = _count +45;
    [self.view addSubview:myImg];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
